package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.wxxymaker.edu.model.entity.Course;
import com.wxxymaker.edu.model.entity.Task;
import com.wxxymaker.edu.model.entity.User;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;
import io.github.biezhi.anima.core.Joins;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: zhanghuan
 * @Date: 2018/10/13 10:46 PM
 * @DESCRIBE:
 */
@Slf4j
@Bean
public class AdminService implements Service {
    /**
     * @param page 页码
     * @param size 分页大小
     * @return
     */
    public Page<User> allUsers(int page, int size) {
        return Anima.select().from(User.class)
                .order(User::getId, OrderBy.DESC)
                .page(page, size);
    }

    /**
     * @param id 根据id审核状态是否通过 以及 删除(暂时不用)
     * @return
     */
    public User audit(int id) {
        return Anima.select().from(User.class)
                .where("id", id).one();
    }


    /**
     * 新上视频展示
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Task> vedioAuidt(int page, int size) {
        Page<Task> taskPage = Anima.select().from(Task.class)
                .order(Task::getId, OrderBy.DESC)
                .page(page, size);

        if (taskPage != null && taskPage.getTotalRows() > 0) {
            taskPage.getRows().forEach(task -> {
                Course course = Anima.select().from(Course.class)
                        .join(Joins.with(User.class).as(Course::getUser).on(Course::getUserId, User::getId))
                        .byId(task.getCourseId());
                task.setCourse(course);
            });
            return taskPage;
        }
        return null;
    }

    /**
     * @param id 根据tid找到审核
     * @return
     */
    public Task video(int id) {
        return Anima.select().from(Task.class)
                .where("id", id).one();

    }


    /**
     * @param category 类别 是0邮箱 1账号 2name 为null时直接分页查询
     * @param content 值
     * @param page
     * @param size
     * @return
     */
    public Page<User> search(Integer category, String content, int page, int size, int noUid) {
        AnimaQuery<User> from = Anima.select().from(User.class);
        if (noUid > 0) {
            from.where("id != " + noUid);
        }

        if (category == null) {
            return from.order(User::getId, OrderBy.DESC)
                    .page(page, size);
        }

        if (category == 0) {
            return from.like(User::getEmail, "%" + content + "%")
                    .order(User::getId, OrderBy.DESC)
                    .page(page, size);
        } else if (category == 1) {
            return from.like(User::getUsername, "%" + content + "%")
                    .order(User::getId, OrderBy.DESC)
                    .page(page, size);
        } else {
            return from.like(User::getName, "%" + content + "%")
                    .order(User::getId, OrderBy.DESC)
                    .page(page, size);
        }
    }


    @Override
    public Object add(Object o) {
        return null;
    }

    @Override
    public Object delete(Object pk) {
        return null;
    }

    @Override
    public boolean change(Object o) {
        return false;
    }

    @Override
    public User byId(Object pk) {
        return null;
    }


}
